<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 安装主程序
// +----------------------------------------------------------------------

namespace app\admin\command;

use app\api\model\AccessTokenModel;
use app\api\model\DouYinService;
use app\api\model\Order;
use app\common\model\ItemModel;
use app\common\model\DouyinModel;
use PDO;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\facade\Log;
use think\facade\Request;
use think\facade\View;
use util\File;
use util\Random;

class Item extends Command
{
    /**
     * @var \think\Request Request 实例
     */
    protected $request;

    protected function configure()
    {
        $this->setName('item')
            ->setDescription('Command Close');
    }

    /**
     * 命令行安装
     */
    protected function execute(Input $input, Output $output)
    {

        echo "开始执行" . "\n";

        try {

            //查询用户数据
            $result = (new DouyinModel())
                ->alias('m')
                ->join('yzn_access_token a', 'a.dy_id = m.id')
                ->field('m.id,m.openid,a.access_token,a.expires_in,a.refresh_token,a.refresh_expires_in,m.nickname')
                ->select();


            $douyin = new DouYinService();

            foreach ($result as $row) {
                echo $row['id']."\n";
                $refresh_token = $row['refresh_token'];
                $access_token = $row['access_token'];
                $openid = $row['openid'];
                if ($row['refresh_expires_in'] < time()) {
                    //刷新refresh_token
                    $res = $douyin->getNewRefreshToken($refresh_token);
                    if ($res) {
                        //更新refresh_token
                        $expires_in = time() + $res['expires_in'] - 3600 * 12;
                        $refresh_token = $res['refresh_token'];
                        (new AccessTokenModel())->where(['dy_id' => $row['id']])
                            ->update(['refresh_token' => $res['refresh_token'], 'refresh_expires_in' => $expires_in]);
                    }
                }
                if ($row['expires_in'] < time()) {
                    //刷新access_token
                    $res = $douyin->getNewAccessToken($refresh_token);
                    if ($res) {
                        $expires_in = time() + $res['expires_in'] - 3600 * 12;
                        $access_token = $res['access_token']
                        //更新refresh_token
                        (new AccessTokenModel())->where(['dy_id' => $row['id']])
                            ->update(['refresh_token' => $access_token, 'expires_in' => $expires_in]);
                    }
                }
                //查询视频数据
                $item = (new ItemModel())->where(['status' => 1, 'dy_id' => $row['id']])->field('id,uid,source_id,item_id')->column('item_id');
                $item = array_filter($item);
                if ($item) {
                    $list = $douyin->getItemInfo($openid, $access_token, $item);
                    if ($list) {
                        foreach ($list as $key => $value) {
                            $videoInfo['cover'] = $value['cover'];
                            $videoInfo['kind'] = $value['media_type'];
                            $videoInfo['comment_count'] = $value['statistics']['comment_count'];
                            $videoInfo['digg_count'] = $value['statistics']['digg_count'];
                            $videoInfo['download_count'] = $value['statistics']['download_count'];
                            $videoInfo['play_count'] = $value['statistics']['play_count'];
                            $videoInfo['share_count'] = $value['statistics']['share_count'];
                            $videoInfo['update_time'] = time();
                            (new ItemModel())->where(['item_id' => $value['item_id']])->update($videoInfo);
                        }
                    }
                }
            }
            echo "执行成功";

        } catch (Exception $e) {
            print_r($e->getMessage());die;
            Log::mylog('item',$e->getMessage(),'item');die;

        }

    }
}
